package com.aigou.admin.aigouspringboot.controller.user;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.ShopCartProduct;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ShopCartProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController("/userShopCartProductController")
@RequestMapping("/user/shopCartProduct")
public class ShopCartProductController {

    @Resource
    private ShopCartProductService shopCartProductService;

    @GetMapping("/user/{userId}")
    @LogScope(title = "购物车商品管理模块", content = "根据userid查询购物车商品信息")
    public Result get(@PathVariable String userId) {

        List<ShopCartProduct> shopCartProductList = shopCartProductService.selectByUserID(userId);
        return Result.success(shopCartProductList);
    }

    // 根据购物车ID批量删除商品数量
    @DeleteMapping("/batchDelete/{ids}")
    public Result batchDelete(@PathVariable String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int i = shopCartProductService.deleteByIds(idList);
        return i > 0 ? Result.success() : Result.error();
    }


    // 根据购物车ID更新商品数量
    @PutMapping("/updateProductNum/{id}")
    public Result edit(@PathVariable String id, @RequestParam Integer productNum) {
        int updatedRows = shopCartProductService.updateProductNum(id, productNum);
        return updatedRows > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "购物车商品管理模块", content = "根据id删除购物车商品信息")
    public Result delete(@PathVariable String id) {

        int i = shopCartProductService.deleteById(id);
        return i > 0 ? Result.success() : Result.error();
    }


}
